package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListModel;
import javax.swing.SpinnerListModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSpinner;

import database.DBHelper;

/**
 * Register User Frame
 * @author warrenshenk
 */
public class RegisterUserFrame extends JFrame {


////////////////////////////////////////////////////////////////////////////////////////////////////
// INSTANCE VARIABLES
////////////////////////////////////////////////////////////////////////////////////////////////////	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane, panel_0, panel_1, panel_2, panel_3, panel_4;
	private JTextField userField, pass1Field, pass2Field;
	private JLabel newUserLabel, password1Label, password2Label, userTypeLabel;
	private JButton createUserBttn, cancelUserBttn;
	private JList userTypeList;
	private DBHelper db;

	
////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * Create the frame.
	 */
	public RegisterUserFrame() {
		db = new DBHelper();
		
		setTitle("Register New User");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(6, 1, 0, 0));
		
		panel_0 = new JPanel();
		contentPane.add(panel_0);
		
		newUserLabel = new JLabel("Username: ");
		panel_0.add(newUserLabel);
		
		userField = new JTextField();
		panel_0.add(userField);
		userField.setColumns(10);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1);
		
		password1Label = new JLabel("Password: ");
		panel_1.add(password1Label);
		
		pass1Field = new JTextField();
		panel_1.add(pass1Field);
		pass1Field.setColumns(10);
		
		panel_2 = new JPanel();
		contentPane.add(panel_2);
		
		password2Label = new JLabel("Confirm Password: ");
		panel_2.add(password2Label);
		
		pass2Field = new JTextField();
		panel_2.add(pass2Field);
		pass2Field.setColumns(10);
		
		panel_3 = new JPanel();
		contentPane.add(panel_3);
		
		userTypeLabel = new JLabel("User Type");
		panel_3.add(userTypeLabel);
		
		userTypeList = new JList(new String[]{"Student", "Instructor"});
		panel_3.add(userTypeList);
		
		panel_4 = new JPanel();
		contentPane.add(panel_4);
		
		createUserBttn = new JButton("Register");
		panel_4.add(createUserBttn);
		createUserBttn.addActionListener(registerListener);
		
		cancelUserBttn = new JButton("Cancel");
		panel_4.add(cancelUserBttn);
		cancelUserBttn.addActionListener(cancelBttnListener);
	}
	
	
////////////////////////////////////////////////////////////////////////////////////////////////////
// BUTTON LISTENERS
////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * Cancel Registration Button Listener
	 */
	private ActionListener cancelBttnListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			LoginFrame lgFrame = new LoginFrame();
			lgFrame.setVisible(true);
			RegisterUserFrame.this.dispose();
		}
		
	};
	//---------------------------------------------------------------------------------------------
	
	/**
	 * Register Button Listener
	 */
	private ActionListener registerListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg1) {
			db.open();

			if (!db.isUsernameUnique(userField.getText())) 
				new JOptionPane().showMessageDialog(RegisterUserFrame.this, "Username is already taken");
			
			else if (!(userField.getText().toString().length() >= 3)) 
				new JOptionPane().showMessageDialog(RegisterUserFrame.this, "Username must be at least 3 characters long");	
					
			else if (!(pass1Field.getText().equals(pass2Field.getText()))) 
				new JOptionPane().showMessageDialog(RegisterUserFrame.this, "Passwords are not Equal");
						
			else if (!(pass1Field.getText().toString().length() >= 3))
				new JOptionPane().showMessageDialog(RegisterUserFrame.this, "Password must be at least 3 characters long");
			
			else if (userTypeList.isSelectionEmpty()) 
				new JOptionPane().showMessageDialog (RegisterUserFrame.this, "Select a User Type");
			else	{
		
				db.insertNewRegUser(userField.getText(), pass1Field.getText(), userTypeList.getSelectedValue().toString());		
				LoginFrame lgFrame = new LoginFrame();
				lgFrame.setVisible(true);
				RegisterUserFrame.this.dispose();
			}
			db.close();
		}
	};
	//---------------------------------------------------------------------------------------------


}
